<?php
$isReloadable = false;
require_once("../../../pers/inc.config.php");
require_once('../../_inc/inc.session.php');
require_once('../../_inc/funct.html.php');
require_once('../../_inc/funct.tabDati.php');
require_once('../../_inc/class.rglDocGen.php');

$sqlSel = "SELECT a.rag_soc, a.sede, a.cod_fisc, a.cod_cciaa,  r.* "
		.", rb.id_mod_rbil , rb.cod_mod_rbil, rb.punteggio AS rb_punteggio, rb.sigla AS rb_sigla"
		.", rb.str_dati AS rb_str_dati, rb.str_def_dati AS rb_str_def_dati, rb.data_ora AS rb_data_ora "
		.", rq.id_mod_rque , rq.cod_mod_rque, rq.punteggio AS rq_punteggio, rq.sigla AS rq_sigla"
		.", rq.str_que AS rq_str_que, rq.data_ora AS rq_data_ora "
		." FROM rgl_rating r "
		." INNER JOIN rgl_azienda a ON a.id_azienda = r.id_azienda " 
		." INNER JOIN rgl_ratbil rb ON rb.id_rating = r.id_rating " 
		." INNER JOIN rgl_ratque rq ON rq.id_rating = r.id_rating " 
		." WHERE r.id_rating = ".reqVarAS('id');
$objDB->query($sqlSel,'selRec');
if (!$acAR = $objDB->fetch('selRec')) {
	die("rquePdf az - var. id '".reqVar('id')."' mancante o errato");
}

$oDoc = new rglDocGen();
$oDoc->maxCol = 40;
$oDoc->pdfIntestaz = $acAR['rag_soc']." - Rating globale ".YMD2DMY($acAR['data_rif']);
$oDoc->bottomLeftText = date("d/m/Y H:i:s")." (".$_SERVER['HTTP_HOST'].RGL_URL_ROOT.")";

$oDoc->primaPag = true;
$oDoc->nuovaRiga();
$aCol = array('cla'=>'tit1','col'=>40,'alg'=>'C','txt'=>'AZIENDA');
$oDoc->setCol($aCol);
$oDoc->nuovaRiga();
$aCol = array('cla'=>'lab1','col'=>6,'alg'=>'L','txt'=>'Ragione sociale');
$oDoc->setCol($aCol);
$aCol = array('cla'=>'txt1','col'=>22,'alg'=>'L','txt'=>$acAR['rag_soc']);
$oDoc->setCol($aCol);
$aCol = array('cla'=>'lab1','col'=>6,'alg'=>'L','txt'=>'Cod.fiscale');
$oDoc->setCol($aCol);
$aCol = array('cla'=>'txt1','col'=>6,'alg'=>'L','txt'=>$acAR['cod_fisc']);
$oDoc->setCol($aCol);
$oDoc->nuovaRiga();
$aCol = array('cla'=>'lab1','col'=>6,'alg'=>'L','txt'=>'Residenza');
$oDoc->setCol($aCol);
$aCol = array('cla'=>'txt1','col'=>22,'alg'=>'L','txt'=>$acAR['sede']);
$oDoc->setCol($aCol);
$aCol = array('cla'=>'lab1','col'=>6,'alg'=>'L','txt'=>'Cod.CCIAA');
$oDoc->setCol($aCol);
$aCol = array('cla'=>'txt1','col'=>6,'alg'=>'L','txt'=>$acAR['cod_cciaa']);
$oDoc->setCol($aCol);

$oDoc->nuovaRiga();
$aCol = array('cla'=>'tit1','col'=>40,'alg'=>'C','txt'=>'RATING');
$oDoc->setCol($aCol);

$sqlSel = "SELECT * FROM rgl_puntglob " 
		." WHERE id_mod_rglob = ".$acAR['id_mod_rglob']
		." AND sigla = '".addslashes($acAR['sigla'])."' ";
$objDB->query($sqlSel,'selRec');
$sStyle = '';
if ($acP = $objDB->fetch('selRec')) {
	if ($acP['colore']=='verde') {
		$sStyle = "color:".RGL_COLORE_VERDE.";";
	} elseif ($acP['colore']=='giallo') {
		$sStyle = "color:".RGL_COLORE_GIALLO.";";
	} else {
		$sStyle = "color:".RGL_COLORE_ROSSO.";";
	}
} 
$oDoc->nuovaRiga();
$aCol = array('cla'=>'txt1','col'=>4,'alg'=>'C','txt'=>$acAR['sigla']);
$oDoc->setCol($aCol);
$aCol = array('cla'=>'txt1','col'=>36,'alg'=>'L','txt'=>$acP['descr_estesa']);
$oDoc->setCol($aCol);
if (!empty($acAR['nota'])) {
	$oDoc->nuovaRiga();
	$aCol = array('cla'=>'txt1','col'=>40,'alg'=>'L','txt'=>$acAR['nota']);
	$oDoc->setCol($aCol);
}
if (!empty($acAR['cod_mod_rbil'])) {
	$aVar = unsgzu($acAR['rb_str_dati']);
	$aDefDati = unsgzu($acAR['rb_str_def_dati']);
	
	$oDoc->nuovaRiga();
	$txt = "Rating bilancio ".YMD2DMY($aVar['B_DATA_RIF'])." - ".YMD2DMY($aVar['C_DATA_RIF']);
	$aCol = array('cla'=>'tit1','col'=>40,'alg'=>'C','txt'=>$txt);
	$oDoc->setCol($aCol);
	
	$sqlSel = "SELECT * FROM rgl_puntbil " 
			." WHERE id_mod_rbil = ".$acAR['id_mod_rbil']
			." AND sigla = '".addslashes($acAR['rb_sigla'])."' ";
	$objDB->query($sqlSel,'selRec');
	$sStyle = '';
	if ($acP = $objDB->fetch('selRec')) {
		if ($acP['colore']=='verde') {
			$sStyle = "color:".RGL_COLORE_VERDE.";";
		} elseif ($acP['colore']=='giallo') {
			$sStyle = "color:".RGL_COLORE_GIALLO.";";
		} else {
			$sStyle = "color:".RGL_COLORE_ROSSO.";";
		}
	} 	
	$oDoc->nuovaRiga();
	$aCol = array('cla'=>'txt1','col'=>4,'alg'=>'C','txt'=>$acAR['rb_sigla']);
	$oDoc->setCol($aCol);
	$aCol = array('cla'=>'txt1','col'=>36,'alg'=>'L','txt'=>$acP['descr_estesa']);
	$oDoc->setCol($aCol);
	
	$oDoc->nuovaRiga();
	$aCol = array('cla'=>'lab1','col'=>20,'alg'=>'C','txt'=>'Indice');
	$oDoc->setCol($aCol);
	$aCol = array('cla'=>'lab1','col'=>5,'alg'=>'C','txt'=>'Valore');
	$oDoc->setCol($aCol);
	$aCol = array('cla'=>'lab1','col'=>10,'alg'=>'C','txt'=>'Valutazione');
	$oDoc->setCol($aCol);
	$aCol = array('cla'=>'lab1','col'=>5,'alg'=>'C','txt'=>'Media settore');
	$oDoc->setCol($aCol);
		
	$sSql = "SELECT ri.* "
		." FROM rgl_ratbil_indici ri "
		." WHERE ri.id_rating = ".reqVar('id');
	$objDB->query($sSql);
	while ($acRBI = $objDB->fetch()) {
		$codH = $acRBI['cod_dato'];
		$desH = $aDefDati[$acRBI['cod_dato']]['descr'];
		$valH = number_format($acRBI['valore'],2,',','.');
		$medH = number_format($acRBI['media_rif'],2,',','.');
		$punH = $acRBI['punteggio'];
		$pesH = $acRBI['peso'];		
		if ($acRBI['punteggio'] > 80) {
			$sValutaz = 'molto positivo';
			$sStyle = 'color:'.RGL_COLORE_VERDE.';';
		} elseif ($acRBI['punteggio'] > 60) {
			$sValutaz = 'positivo';
			$sStyle = 'color:'.RGL_COLORE_VERDE.';';
		} elseif ($acRBI['punteggio'] > 30) {
			$sValutaz = 'in media';
			$sStyle = 'color:'.RGL_COLORE_GIALLO.';';
		} elseif ($acRBI['punteggio'] > 10) {
			$sValutaz = 'negativo';
			$sStyle = 'color:'.RGL_COLORE_ROSSO.';';
		} else {
			$sValutaz = 'molto negativo';
			$sStyle = 'color:'.RGL_COLORE_ROSSO.';';
		}
		$oDoc->nuovaRiga();
		$aCol = array('cla'=>'txt1','col'=>20,'alg'=>'L','txt'=>$desH);
		$oDoc->setCol($aCol);
		$aCol = array('cla'=>'txt1','col'=>5,'alg'=>'R','txt'=>$valH);
		$oDoc->setCol($aCol);
		$aCol = array('cla'=>'txt1','col'=>10,'alg'=>'C','txt'=>$sValutaz);
		$oDoc->setCol($aCol);
		$aCol = array('cla'=>'txt1','col'=>5,'alg'=>'R','txt'=>$medH);
		$oDoc->setCol($aCol);
	}	
}
if (!empty($acAR['cod_mod_rque'])) {
	$aQueRisp = array();
	if (!empty($acAR['rq_str_que'])) {
		$aQueRisp = unsgzu($acAR['rq_str_que']);
	}	
	$oDoc->nuovaRiga();
	$aCol = array('cla'=>'tit1','col'=>40,'alg'=>'C','txt'=>'Rating questionario');
	$oDoc->setCol($aCol);

	$sqlSel = "SELECT * FROM rgl_puntque " 
			." WHERE id_mod_rque = ".$acAR['id_mod_rque']
			." AND sigla = '".addslashes($acAR['rq_sigla'])."' ";
	$objDB->query($sqlSel,'selRec');
	$sStyle = '';
	if ($acP = $objDB->fetch('selRec')) {
		if ($acP['colore']=='verde') {
			$sStyle = "color:".RGL_COLORE_VERDE.";";
		} elseif ($acP['colore']=='giallo') {
			$sStyle = "color:".RGL_COLORE_GIALLO.";";
		} else {
			$sStyle = "color:".RGL_COLORE_ROSSO.";";
		}
	} 
	$oDoc->nuovaRiga();
	$aCol = array('cla'=>'txt1','col'=>4,'alg'=>'C','txt'=>$acAR['rq_sigla']);
	$oDoc->setCol($aCol);
	$aCol = array('cla'=>'txt1','col'=>36,'alg'=>'L','txt'=>$acP['descr_estesa']);
	$oDoc->setCol($aCol);
	
	$oDoc->nuovaRiga();
	$aCol = array('cla'=>'lab1','col'=>40,'alg'=>'C','txt'=>'Questionario');
	$oDoc->setCol($aCol);
	
	$nS=0;
	foreach($aQueRisp as $codSez => $aSez) {
		$nS++;
		$nD=0;
		$oDoc->nuovaRiga();
		$aCol = array('cla'=>'lab1','col'=>40,'alg'=>'L','txt'=>$nS.". ".$aSez['d']);
		$oDoc->setCol($aCol);
		foreach($aSez['a'] as $codDom => $aDom) {
			$nD++;
			$oDoc->nuovaRiga();
			$aCol = array('cla'=>'txt1','col'=>2,'alg'=>'R','txt'=>$nD.". ");
			$oDoc->setCol($aCol);
			$aCol = array('cla'=>'txt1','col'=>32,'alg'=>'L','txt'=>$aDom['d']);
			$oDoc->setCol($aCol);
			$aCol = array('cla'=>'txt1','col'=>6,'alg'=>'L','txt'=>$aDom['r']);
			$oDoc->setCol($aCol);
		}
	}
}
$sPdf = $oDoc->genPdf(array('orientaPagina'=>'P'));
headerDownLoad(strlen($sPdf),"rating_".$acAR['cod_fisc']."_".$acAR['data_rif'].".pdf");
echo $sPdf;
exit();
